class MoviesController < ApplicationController
  before_action :set_movie, only: [:show]

  def index
    list_filter_scope
    @movies = @movies.movie.page(params[:page])
    if !@titles.blank?
      page_title = @titles.join(" - ")
    else
      page_title = "电影"
    end
    set_seo_meta(page_title, @titles.join(","))
    fresh_when(:etag => [@titles,@movies])
  end

  def series
    list_filter_scope
    @movies = @movies.tv.page(params[:page])
    if !@titles.blank?
      page_title = @titles.join(" - ")
    else
      page_title = "电视剧"
    end
    set_seo_meta(page_title, @titles.join(","))
    fresh_when(:etag => [@titles,@movies])
  end

  def top
    params[:sort] = 'rank'
    list_filter_scope
    @movies = @movies.limit(250)
    params.delete(:sort)
    @movies = Kaminari.paginate_array(@movies.to_a, total_count: 250).page(params[:page]).per(20)
    fresh_when(:etag => [@movies])
  end

  def search
    @titles = []
    @movies = Movie.search do
      fulltext params[:q]
      with(:verified,false)
      with(:countries,params[:country]) unless params[:country].blank?
      with(:categories,params[:category]) unless params[:category].blank?
      with(:directors,params[:director]) unless params[:director].blank?
      with(:actors,params[:actor]) unless params[:actor].blank?
      with(:year,params[:year]) unless params[:year].blank?
      paginate page: params[:page], per_page: 20
    end.results
    @titles << params[:country] unless params[:country].blank?
    @titles << params[:category] unless params[:category].blank?
    @titles << params[:director] unless params[:director].blank?
    @titles << params[:actor] unless params[:actor].blank?
    @titles << params[:year] unless params[:year].blank?
    set_seo_meta(@titles.join(" - "), @titles.join(","))
    fresh_when(:etag => [@titles,@movies])
  end

  def show
    @titles = []
    @titles << @movie.title
    @titles << @movie.title_en
    @titles << @movie.aliases.join(",")
    @recommand = Movie.verified
    if(!@movie.categories.blank? && !@movie.actors.blank?)
      @recommand = @recommand.tagged_with_on(:categories, @movie.categories[0,3], :match => :any)
      @recommand = @recommand.tagged_with_on(:actors, @movie.actors[0,3], :match => :any)
    elsif(!@movie.categories.blank?)
      @recommand = @recommand.tagged_with_on(:categories, @movie.categories[0,3], :match => :any)
    elsif(!@movie.actors.blank?)
      @recommand = @recommand.tagged_with_on(:actors, @movie.actors[0,3], :match => :any)
    end
    @recommand = @recommand.and(:_id.ne => @movie.id).desc(:score).limit(10)
    set_seo_meta(@titles.join(" - "), @titles.join(","),@titles.join(","))
    if @movie.type == 'movie'
      @current = [movies_path]
    else
      @current = [series_movies_path]
    end
    fresh_when(:etag => [@titles,@movie,@recommand,@current])
  end


  def suggest
    @movie = Movie.find(params[:id])
    case params[:type]
    when "1"
      Home.push_suggest(@movie)
    when "0"
      Home.push_movie(@movie)
    end
    render :text => "1"
  end

  def spider
    @movie = Movie.find(params[:id])
    @text = Spider.spider_bt(@movie)
    #render :text => @text
    redirect_to action: "show", id: params[:id]
  end

  def fetch_douban
    @movie = Movie.find(params[:id])
    Spider.spider_douban_html(@movie.source_id)
    redirect_to action: "show", id: params[:id]
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_movie
    @movie = Movie.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def movie_params
    params[:movie]
  end
end
